/**
 * 
 */
package view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.border.Border;
import javax.swing.border.TitledBorder;

import model.GameDriver;
import model.Item;

import common.DataManager;
import common.FontFactory;
import common.ItemFactory;

import controller.Controller;

/**
 * @author Bill
 * 
 * Panel representing the view of the "Store" class. Contains a
 * label which shows the player's current points, several sections 
 * having many clickable icons of buyable items, and a button. The user can click
 * on the button to start the next wave of enemies, or click on a item image to buy the item. 
 * Is used as part of the inventory panel.
 *
 */
public class StorePanel extends JPanel {
	
	private static final int STORE_WIDTH = 250;
	private static final int STORE_HEIGHT = 500;
	private static final int STORE_SECTIONS_WIDTH = STORE_WIDTH*9/10;
	private static final int STORE_SECTIONS_HEIGHT = STORE_HEIGHT*4/5;
	private static final int POINTS_LABEL_WIDTH = STORE_WIDTH;
	private static final int POINTS_LABEL_HEIGHT = STORE_HEIGHT/10;
	private static final int START_BUTTON_WIDTH = STORE_WIDTH;
	private static final int START_BUTTON_HEIGHT = STORE_HEIGHT/10;
	private static final int STORE_ITEM_HGAP = 20;
	private static final int STORE_ITEM_VGAP = 10;
	
	private static int NUM_STORE_SECTIONS = 4;
	
	private JPanel storeSectionsPanel = new JPanel(null);
	JLabel moneyLabel = new JLabel("$XXX", JLabel.CENTER);
	
	/**
	 * Constructs the store panel
	 */
	public StorePanel(){
		super();
		this.setLayout(new BorderLayout());
		this.setOpaque(false);
		
		GridBagLayout storeSectionsPanelLayout = new GridBagLayout();
		GridBagConstraints constraints = new GridBagConstraints();
		storeSectionsPanel.setLayout(storeSectionsPanelLayout);
		storeSectionsPanel.setBackground(new Color(0.2f,0.2f,0.2f,0.2f));
		//storeSectionsPanel.setOpaque(false);
		
		Color sectionBgColor = new Color(0.4f,0.4f,0.4f,0.7f);
		Border innerBorder = BorderFactory.createLoweredBevelBorder();
		Border outerBorder = BorderFactory.createLineBorder(Color.DARK_GRAY,3);
		Border compoundBorder = BorderFactory.createCompoundBorder(outerBorder, innerBorder);
		FontFactory fontFactory = new FontFactory();
		TitledBorder section0Border = BorderFactory.createTitledBorder(compoundBorder, "Manual Weapons", TitledBorder.CENTER, TitledBorder.TOP, fontFactory.createFont(12),Color.white);
		TitledBorder section1Border = BorderFactory.createTitledBorder(compoundBorder, "Automatic Weapons", TitledBorder.CENTER, TitledBorder.TOP, fontFactory.createFont(12),Color.white);
		TitledBorder section2Border = BorderFactory.createTitledBorder(compoundBorder, "Defensive Items", TitledBorder.CENTER, TitledBorder.TOP, fontFactory.createFont(12),Color.white);
		TitledBorder section3Border = BorderFactory.createTitledBorder(compoundBorder, "Upgrades", TitledBorder.CENTER, TitledBorder.TOP, fontFactory.createFont(12),Color.white);
		
		GridLayout section0Layout = new GridLayout(DataManager.getManualWeaponIcons().size()/2,DataManager.getManualWeaponIcons().size());
		section0Layout.setHgap(STORE_ITEM_HGAP);
		section0Layout.setVgap(STORE_ITEM_VGAP);
		JPanel storeSection0 = new JPanel(section0Layout);
		storeSection0.setBorder(section0Border);
		storeSection0.setBackground(sectionBgColor);
		for(int j = 0; j < DataManager.getManualWeaponIcons().size(); j++){
			Item item = ItemFactory.createItemFromImageName(DataManager.getManualWeaponIcons().get(j));
			StoreItemPanel itemPanel = new StoreItemPanel(DataManager.getManualWeaponIcons().get(j), "$"+item.getCost());
			storeSection0.add(itemPanel);
		}
		constraints.gridy = 0;
		constraints.weighty = 1.0;
		storeSectionsPanel.add(storeSection0, constraints);
		storeSection0.setLocation(0,0);
		
		GridLayout section1Layout = new GridLayout(DataManager.getAutomaticWeaponIcons().size()/2,DataManager.getAutomaticWeaponIcons().size());
		section1Layout.setHgap(STORE_ITEM_HGAP);
		section1Layout.setVgap(STORE_ITEM_VGAP);
		JPanel storeSection1 = new JPanel(section1Layout);
		storeSection1.setBorder(section1Border);
		storeSection1.setBackground(sectionBgColor);
		for(int j = 0; j < DataManager.getAutomaticWeaponIcons().size(); j++){
			Item item = ItemFactory.createItemFromImageName(DataManager.getAutomaticWeaponIcons().get(j));
			StoreItemPanel itemPanel = new StoreItemPanel(DataManager.getAutomaticWeaponIcons().get(j), "$"+item.getCost());
			storeSection1.add(itemPanel);
		}
		constraints.gridy = 1;
		constraints.weighty = 1.0;
		storeSectionsPanel.add(storeSection1, constraints);
		storeSection1.setLocation(0,(STORE_SECTIONS_HEIGHT/NUM_STORE_SECTIONS));
		
		GridLayout section2Layout = new GridLayout(DataManager.getDefensiveItemIcons().size()/2,DataManager.getDefensiveItemIcons().size());
		section2Layout.setHgap(STORE_ITEM_HGAP);
		section2Layout.setVgap(STORE_ITEM_VGAP);
		JPanel storeSection2 = new JPanel(section2Layout);
		storeSection2.setBorder(section2Border);
		storeSection2.setBackground(sectionBgColor);
		for(int j = 0; j < DataManager.getDefensiveItemIcons().size(); j++){
			Item item = ItemFactory.createItemFromImageName(DataManager.getDefensiveItemIcons().get(j));			
			StoreItemPanel itemPanel = new StoreItemPanel(DataManager.getDefensiveItemIcons().get(j), "$"+item.getCost());
			storeSection2.add(itemPanel);
		}
		constraints.gridy = 2;
		constraints.weighty = 1.0;
		storeSectionsPanel.add(storeSection2, constraints);
		storeSection2.setLocation(0,2*(STORE_SECTIONS_HEIGHT/NUM_STORE_SECTIONS));
		
		GridLayout section3Layout = new GridLayout(DataManager.getUpgradeIcons().size()/2,DataManager.getUpgradeIcons().size());
		section3Layout.setHgap(STORE_ITEM_HGAP);
		section3Layout.setVgap(STORE_ITEM_VGAP);
		JPanel storeSection3 = new JPanel(section3Layout);
		storeSection3.setBorder(section3Border);
		storeSection3.setBackground(sectionBgColor);
		for(int j = 0; j < DataManager.getUpgradeIcons().size(); j++){
			Item item = ItemFactory.createItemFromImageName(DataManager.getUpgradeIcons().get(j));			
			StoreItemPanel itemPanel = new StoreItemPanel(DataManager.getUpgradeIcons().get(j), "$"+item.getCost());
			storeSection3.add(itemPanel);
		}
		constraints.gridy = 3;
		constraints.weighty = 1.0;
		storeSectionsPanel.add(storeSection3, constraints);
		storeSection3.setLocation(0,3*(STORE_SECTIONS_HEIGHT/NUM_STORE_SECTIONS));
		
		moneyLabel.setForeground(Color.WHITE);
		moneyLabel.setPreferredSize(new Dimension(POINTS_LABEL_WIDTH,POINTS_LABEL_HEIGHT));
		
		JButton startWaveButton = new JButton("Begin");
		startWaveButton.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				Controller.advanceState();
			}
		});
		startWaveButton.setOpaque(false);
		startWaveButton.setForeground(Color.BLACK);
		startWaveButton.setPreferredSize(new Dimension(START_BUTTON_WIDTH,START_BUTTON_HEIGHT));
		
		storeSectionsPanel.setSize(new Dimension(STORE_SECTIONS_WIDTH,STORE_SECTIONS_HEIGHT));
		this.add(storeSectionsPanel, BorderLayout.CENTER);
		this.add(moneyLabel,BorderLayout.NORTH);
		this.add(startWaveButton, BorderLayout.SOUTH);
		this.setSize(STORE_WIDTH,STORE_HEIGHT);
		
	}
	
	/**
	 * unhighlights all of the store items
	 */
	public void unhighlightAllItems(){
		for(int i = 0; i < storeSectionsPanel.getComponentCount(); i++){
			JPanel section = (JPanel)storeSectionsPanel.getComponent(i);
			for(int j = 0; j < section.getComponentCount(); j++){
				SmallItemIconPanel item = (SmallItemIconPanel)section.getComponent(j);
				item.unhighlightItem();
			}
		}
	}
	
	/**
	 * finds a particular itemIconPanel from it's image name
	 */
	public SmallItemIconPanel findItemIconFromName(String name){
		for(int i = 0; i < storeSectionsPanel.getComponentCount(); i++){
			JPanel section = (JPanel)storeSectionsPanel.getComponent(i);
			for(int j = 0; j < section.getComponentCount(); j++){
				SmallItemIconPanel item = (SmallItemIconPanel)section.getComponent(j);
				if(item.getItemName() == name) return item;
			}
		}
		return null;
	}
	
	/**
	 * Syncs the data displayed in the store view with the game driver (model) data.
	 * 
	 * @param gameDriver
	 */
	public void updateStore(GameDriver gameDriver){
		moneyLabel.setText("$"+gameDriver.getPlayer().getPoints());
		unhighlightAllItems();
		if(gameDriver.getStore().isItemSelected()){
			System.out.println("Selected item is "+gameDriver.getStore().getSelectedItem());
			SmallItemIconPanel item = findItemIconFromName(gameDriver.getStore().getSelectedItem());
			item.highlightItem();
		}
		for(int i = 0; i < gameDriver.getStore().getBuyableItemsList().size(); i++){
			Item item = gameDriver.getStore().getBuyableItemsList().get(i);
			SmallItemIconPanel itemIcon = findItemIconFromName(item.getImageName());
			if(!gameDriver.getStore().playerCanBuyItem(item, gameDriver.getPlayer()))
				itemIcon.coverItem();
			else itemIcon.uncoverItem();
		}
	}
}
